package JDBC;

import java.sql.*;

public class Text03 {
    public static void main(String[] args) throws Exception{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","kz","root");
        String sql="select * from master";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            System.out.println(resultSet.getInt("userid")+"=>  "+ resultSet.getString("username")+"=>  "+ resultSet.getString("password"));

        }
        resultSet.close();
        statement.close();
        connection.close();

        //手动抛异常

        /*   Connection connection=null;
    Statement statement=null;
    ResultSet resultSet=null;
        try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@127.0.0.1:1521:orcl",
                "kz",
                "root");
                String sql="select * from master";
                statement = connection.createStatement();
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()){
                String tname = resultSet.getString("username");
                String password= resultSet.getString("password");
                System.out.println(tname+"    "+password);
                }

                } catch (Exception e) {
                e.printStackTrace();
                }finally {
                try {
                resultSet.close();
                statement.close();
                connection.close();
                } catch (SQLException e) {
                e.printStackTrace();
                }

                }*/

    }
}
